#include "choose.h"
#include "ui_choose.h"
#include <QPainter>
#include <QDebug>

#include <QPushButton>
#include <QMediaPlayer>
#include <QAudioOutput>
#include "game.h"

Choose::Choose(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Choose),
      gameWidget(nullptr)
{
    ui->setupUi(this);
    player = new QMediaPlayer(this);                  //new一下
    audioOutput = new QAudioOutput(this);
    player->setAudioOutput(audioOutput);             //音乐输出

    player->setSource(QUrl("qrc:///bgm2.mp3"));


    player->setLoops(QMediaPlayer::Infinite);               //循环播放

    audioOutput->setVolume(30);                          //音量

    player->play();



    this->resize(1000, 600);                //设定窗口大小


    QPushButton *songbirds = ui->songbirds; // 确保按钮objectName是pushButton
    connect(songbirds, &QPushButton::clicked, this, &Choose::on_songbirds_clicked);       //跳转至game

    ui->songbirds->setFixedSize(100, 50);
    ui->songbirds->setStyleSheet(
        "QPushButton {"
        "    border-radius: 10px;"          // 圆角半径
        "    background-color: #ADD8E6;"    // 背景色
        "    color: white;"                // 文字颜色
        "}"
        "QPushButton:hover {"
        "    background-color: #FFD1DC;"   // 悬停状态颜色
        "}"
        "QPushButton:pressed {"
        "    background-color: #FFD1DC;"   // 按下状态颜色
        "}"
        );


    ui->aidejueduan->setFixedSize(100, 50);
    ui->aidejueduan->setStyleSheet(
        "QPushButton {"
        "    border-radius: 10px;"
        "    background-color:#FFD1DC ;"
        "    color: white;"
        "}"
        "QPushButton:hover {"
        "    background-color: #ADD8E6;"
        "}"
        "QPushButton:pressed {"
        "    background-color: #ADD8E6;"
        "}"
        );
}


Choose::~Choose()
{
    delete player;
    delete audioOutput;
    delete ui;
    if(gameWidget) delete gameWidget;

}

//跳转至songbirds
void Choose::on_songbirds_clicked(){
    if(!gameWidget) {
        gameWidget = new Game();  // 确保Choose类已正确定义
    }

    this->hide();
    gameWidget->show();
    qDebug() << "Jump to Game window";         // 跳转

    player->pause();
    qDebug() << "Audio paused";                    //跳转至下个窗口时音乐暂停
}



void Choose::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.drawPixmap(0, 0, width(), height(), QPixmap(":/new/prefix1/bg2.2.bmp"));
}                                                  //绘制背景
